$(function(){
  var myChart = echarts.init(document.getElementById('main'));
  
  let xData=[],
      yData = [];

  for(let i=0;i<=1;i+=0.1){
    xData.push(roundFractional(i,1));
    //yData.push(h(i));
    yData.push(roundFractional(-1*(r(t=i)+r(1-t)),2));
  }

  /*
  function h(p){
    return -1*p*Math.log2(p)-(1-p)*Math.log2(1-p);
  }
  */
  
  function roundFractional(x, n) {
    return Math.round(x * Math.pow(10, n)) / Math.pow(10, n);
  }

  function r(t){
    return 0 === t ? 0 : t * Math.log2(t);
  }


  // 指定图表的配置项和数据
  var option = {
    title: {
      text: '二进熵函数曲线'
    },
    tooltip: {},
    legend: {
      data:['H(X)']
    },
    xAxis: {
      data: xData
    },
    yAxis: {},
    series: [{
      name: '销量',
      type: 'line',
      smooth: true,
      data: yData  //H(p)
    }]
  };

  // 使用刚指定的配置项和数据显示图表。
  myChart.setOption(option);
});
